package tiled.simple.gwt.client;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.collision.shapes.ShapeType;
import org.jbox2d.common.Transform;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.World;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;

public class JBox2DGWTTestbed {
	public World world;
	
	public JBox2DGWTTestbed(World world) {
		this.world=world;
	}

	private void line(Context2d context,double x1,double y1,double x2,double y2,CssColor c){
        context.setStrokeStyle(c);
        context.setLineCap("square");
        context.setLineWidth(1);
        context.setLineJoin("bevel");
        context.beginPath();
        context.moveTo(x1*Game.PIXEL_PER_METER,y1*Game.PIXEL_PER_METER);
        context.lineTo(x2*Game.PIXEL_PER_METER,y2*Game.PIXEL_PER_METER);
        context.stroke();
	}
	
	private void circle(Context2d context,double x,double y,double r,CssColor c){
        context.setStrokeStyle(c);
        context.beginPath();
        context.arc(x*Game.PIXEL_PER_METER, y*Game.PIXEL_PER_METER, r*Game.PIXEL_PER_METER, 0, Math.PI * 2.0);
        context.closePath();
        context.stroke();
	}
	
	
	
	public void render(Context2d context) {

		renderBodies(world,context);
	}
	
	private final CssColor SHAPE_NOT_ACTIVE = CssColor.make("rgba(255,255,255, 255)");
	private final CssColor SHAPE_STATIC     = CssColor.make("rgba(128,255,128, 255)");
	private final CssColor SHAPE_KINEMATIC  = CssColor.make("rgba(128,240,240, 255)");
	private final CssColor SHAPE_DYNAMIC    = CssColor.make("rgba(128,128,240, 255)");
	private final CssColor SHAPE_NOT_AWAKE  = CssColor.make("rgba(150,150,150, 255)");
	private final CssColor SHAPE_AWAKE      = CssColor.make("rgba(240,200,200, 255)");
	  
	private void renderBodies (World world,Context2d context) {
		Body body;
		Fixture fixture;
		for (body = world.getBodyList(); body != null; body = body.getNext()) {
			Transform transform = body.getTransform();
			for (fixture = body.getFixtureList(); fixture != null; fixture = fixture.getNext()) {
				if 		(body.isActive() == false) 			       drawShape(fixture, transform, context, SHAPE_NOT_ACTIVE);
			    else if (body.getType() == BodyType.STATIC)        drawShape(fixture, transform, context, SHAPE_STATIC);
		        else if (body.getType() == BodyType.KINEMATIC)     drawShape(fixture, transform, context, SHAPE_KINEMATIC);
		        else if (body.getType() == BodyType.DYNAMIC)       drawShape(fixture, transform, context, SHAPE_DYNAMIC);
			    else if (body.isAwake() == false) 				   drawShape(fixture, transform, context, SHAPE_NOT_AWAKE);
			    else 											   drawShape(fixture, transform, context, SHAPE_AWAKE);
			}
		}
    }

	private void drawShape(Fixture fixture, Transform transform,Context2d context,CssColor rgb) {
		
		if (fixture.getType() == ShapeType.CIRCLE) {
			CircleShape circle = (CircleShape) fixture.getShape();
			float r=circle.m_radius;
			//float a=transform.getAngle();
			Vec2 center = new Vec2(0,0);
			Transform.mulToOut(transform, circle.m_p, center);
			
			//current circle
			circle(context,center.x,center.y,r,rgb);
			
	    } else {
			PolygonShape polygon = (PolygonShape)fixture.getShape();
			Vec2 p1;
			Vec2 p2;
			int i=1;
			for(i=1;i<=polygon.getVertexCount();i++){
				p1=polygon.getVertex(i-1);
				if(i==polygon.getVertexCount()) {p2=polygon.getVertex(0);} else {p2=polygon.getVertex(i);}
				
				p1=Transform.mul(transform, p1);
				p2=Transform.mul(transform, p2);

				line(context,p1.x,p1.y,p2.x,p2.y,rgb);
				
			}
	    }
	}
	
}
